﻿using System;
/*c1. 为示例（参见第4章课件第9页）中的泛型链表类添
 * 加类似于List<T>类的ForEach(Action<T> action)
 * 方法。通过调用这个方法打印链表元素，求最大值、最
 * 小值和求和（使用lambda表达式实现）。*/
namespace HW3_1
{
    class Program
    {


        public class Node<T>
        {
            public Node<T> Next { get; set; }
            public T Data { get; set; }

            public Node(T t)
            {
                Next = null;
                Data = t;
            }
        }

        public class GenericList<T>
        {
            private Node<T> head;
            private Node<T> tail;

            public GenericList()
            {
                tail = head = null;
            }
            public Node<T> Head
            {
                get => head;
            }
            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if (tail == null)
                {
                    head = tail = n;
                }
                else
                {
                    tail.Next = n;
                    tail = n;
                }
            }
            /*//打印
            public void printIt(GenericList<int> intlist)
            {
                int i = 0;
                for (Node<int> node = intlist.Head; node != null; node = node.Next, i++)
                {
                    Console.WriteLine("nodedata{0}:{1}", i + 1, node.Data);
                }
            }
            //求最大
            public int getmax(GenericList<int> intlist)
            {
                int i = intlist.Head.Data;
                for (Node<int> node = intlist.Head; node != null; node = node.Next)
                {
                    if(i <= node.Data)
                        i = node.Data;
                }
                return i;
            }
            //求最小
            public int getmin(GenericList<int> intlist)
            {
                int i = intlist.Head.Data;
                for (Node<int> node = intlist.Head; node != null; node = node.Next)
                {
                    if (i >= node.Data)
                        i = node.Data;
                }
                return i;
            }
            //求和
            public int getsum(GenericList<int> intlist)
            {
                int i = 0;
                for (Node<int> node = intlist.Head; node != null; node = node.Next)
                {
                    i += node.Data;
                }
                return i;
            }*/
            //ForEach方法
            public void ForEach(Action<T> action)
            {
                Node<T> point = head;
                while (point != null)
                {
                    action(point.Data);
                    point = point.Next;
                }
            }


        }
        /*class LambdaTackle
        {
            public static int max(GenericList<int> list)=>
        }*/




        static void Main(string[] args)
        {



            GenericList < int > intlist= new GenericList<int>();
            for(int x=0;x<10;x++)
            {
                intlist.Add(x);
            };
            Console.WriteLine("Data following:");
            int count = 1;
            intlist.ForEach(d => { Console.WriteLine("data {0}:{1} ", count, d); count++; });
            Console.WriteLine("");
            int maxNum = intlist.Head.Data;
            int minNum = intlist.Head.Data;
            int sum = 0;
            intlist.ForEach(d => {
                if (d >= maxNum) maxNum = d;
                if (d <= minNum) minNum = d;
                sum += d;
            });
            Console.WriteLine("Max:{0}\nMin:{1}\nSum{2}", maxNum, minNum, sum);


            //intlist.getall(intlist, intlist.getmax(intlist), intlist.getmin(intlist), intlist.getsum(intlist));
            
        }
    }
}
